package com.javalong.affairs.module.recycle.activity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.javalong.affairs.R;
import com.javalong.affairs.base.activity.UIHandlerActivity;
import com.javalong.affairs.base.common.ServiceLocator;
import com.javalong.affairs.base.common.UIHandler;
import com.javalong.affairs.base.constant.BundleKey;
import com.javalong.affairs.base.constant.RequestCode;
import com.javalong.affairs.base.fragment.BaseFragment;
import com.javalong.affairs.base.model.Dept;
import com.javalong.affairs.module.recycle.fragment.AuditRecycleListFragment;
import com.javalong.affairs.module.recycle.fragment.ReceiveRecycleFragment;
import com.javalong.affairs.module.recycle.fragment.ScheduleRecycleListFragment;
import com.javalong.affairs.module.recycle.fragment.SendRecycleFragment;
import com.javalong.affairs.base.ui.BottomBar;
import com.javalong.affairs.base.ui.BottomBar.OnBottomClick;
import com.javalong.affairs.base.ui.TopTitle;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;

@ContentView(R.layout.activity_recycle)
public class RecycleActivity extends UIHandlerActivity implements OnBottomClick {

	@ViewInject(R.id.ui_toptitle)
	private TopTitle topTitle;
	@ViewInject(R.id.ui_bottombar)
	private BottomBar bottomBar;
	@ViewInject(R.id.ll_root)
	private LinearLayout ll_root;
	private ReceiveRecycleFragment receiveFragment;
	private SendRecycleFragment sendFragment;
	private AuditRecycleListFragment auditListFragment;
	private ScheduleRecycleListFragment scheduleRecycleListFragment;
	private ListView lvPopupList;
	private BaseFragment currFragment;
	private int[] drawables = new int[] { R.drawable.notification_level_f1,
			R.drawable.notification_level_f2, R.drawable.notification_level_f4 };
	private PopupWindow pwMyPopWindow;
	private ArrayList<Map<String, String>> moreList;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ViewUtils.inject(this);
		topTitle.setOnLeftDrawableClick(this);
		bottomBar.setOnBottonClick(this);
		bottomBar.setRes(drawables, null);
		loadOneFragment();
		registerEvent(new int[] { R.string.event_refresh_notification_send,
				R.string.event_refresh_notification_receive,
				R.string.event_refresh_auditlist });
	}

	// 载入首视图
	private void loadOneFragment() {
		FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
		if (receiveFragment == null) {
			receiveFragment = new ReceiveRecycleFragment();
		}
		ft.add(R.id.fl_notify, receiveFragment);
		ft.commit();
		bottomBar.setSelection(0);
	}

	@Override
	public void onBottomClick(int index) {
		FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
		currFragment = null;
		switch (index) {
		case 0:
			if (receiveFragment == null) {
				receiveFragment = new ReceiveRecycleFragment();

			}
			currFragment = receiveFragment;
			break;
		case 1:
			if (sendFragment == null) {
				sendFragment = new SendRecycleFragment();
			}
			currFragment = sendFragment;
			break;
		case 2:
			currFragment = auditListFragment = new AuditRecycleListFragment();
			break;
		}

		ft.replace(R.id.fl_notify, currFragment);
		ft.commit();
	}

	@Override
	public void OnSynCalled(int ulID, Bundle bundle) {
		super.OnSynCalled(ulID, bundle);
		switch (ulID) {
		case R.string.event_refresh_notification_send:
			if (sendFragment != null) {
				sendFragment.refresh();
			}
			break;
		case R.string.event_refresh_notification_receive:
			if (receiveFragment != null) {
				receiveFragment.refresh();
			}

			break;
		case R.string.event_refresh_auditlist:
			if (auditListFragment != null) {
				auditListFragment.refresh();
			}
			break;
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == RESULT_OK) {
			switch (requestCode) {
			case RequestCode.DEPTLIST:
				List<Dept> deptList = (List<Dept>) data
						.getSerializableExtra(BundleKey.DEPTLIST);
				Bundle bundle = new Bundle();
				bundle.putSerializable(BundleKey.DEPTLIST,
						(Serializable) deptList);
				UIHandler uiHandler = ServiceLocator.GetInstance().getInstance(
						UIHandler.class);
				uiHandler.postCallback(R.string.event_add_dept, bundle);
				break;
			}
		}
	}
}
